package com.imau.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {

    @Pointcut("execution(void com.imau.dao.BookDao.update())")
    public void pt(){}
    @Pointcut("execution(int com.imau.dao.BookDao.select())")
    public void pt2(){}
    @Before("pt()")
    public void before()
    {
        System.out.println("before.....");
    }

    @After("pt()")
    public void after()
    {
        System.out.println("after.....");
    }


    @Around("pt()")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around before advice ...");
        Long startTime = System.currentTimeMillis();
        for (int i=0;i<10000;++i)
        {
            joinPoint.proceed(); //调用原有方法
        }
        Long endTime = System.currentTimeMillis();
        System.out.println("around time is "+(endTime-startTime));
        System.out.println("around after advice ...");
    }
   @Around("pt2()")
    public Object aroundReturn(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around before advice ...");
        Object proceed = joinPoint.proceed();
        System.out.println("around after advice ...");

        return proceed;
    }

    @AfterThrowing("pt2()")
    public void afterThrowing()
    {
        System.out.println("afterThrowing.....");
    }

    @AfterReturning("pt2()")
    public void afterReturning()
    {
        System.out.println("afterReturning.....");
    }
}
